package com.adou.service.impl;

import com.adou.common.dto.ConditionDataInDto;
import com.adou.common.dto.InConditionDto;
import com.adou.common.mapper.PhoneMapper;
import com.adou.common.mapper.SearchConditionMapper;
import com.adou.common.model.SearchCondition;
import com.adou.common.util.StringUtil;
import com.adou.service.PhoneService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("PhoneService")
public class PhoneServiceImpl implements PhoneService {

    @Autowired
    private PhoneMapper phoneMapper;

    @Autowired
    private SearchConditionMapper searchConditionMapper;

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> queryTableDeploy() {
        return phoneMapper.queryTableDeploy();
    }

    @Transactional(readOnly = true)
    @Override
    public Map<String, Object> pageList(Map<String, Object> map) {
        Map<String,Object> m = new HashMap<>();
        m.put("sessionid", "");
        m.put("data", "");
        if (StringUtil.isBlank(map.get("formId"))){
            m.put("result", 101);
            m.put("msg","formId为空");
            return m;
        }
        String tableName = phoneMapper.queryFormName(map);
        if(StringUtil.isBlank(tableName)){
            m.put("result", 102);
            m.put("msg","表单不存在");
            return m;
        }
        String fields = phoneMapper.queryFields(map);
        if(StringUtil.isBlank(fields)){
            m.put("result", 103);
            m.put("msg","未查询出在手机端显示字段");
            return m;
        }
        map.put("formName",tableName);
        int count = phoneMapper.countList(map);
        m.put("result", 1);
        if(count==0){
            m.put("msg","数据列表为空");
            return m;
        }
        StringBuffer sql = new StringBuffer();
        sql.append(fields);
        sql.append(",id");
        sql.append(" from "+ map.get("formName"));
        List<Map<String, Object>> listIn = (List<Map<String, Object>>)map.get("conditionList");
        if(listIn.size()>0){
            sql.append(" where 1=1 ");
            for (int i=0;i<listIn.size();i++) {
                Map<String, Object> map1 = listIn.get(i);
                InConditionDto inConditionDto = new InConditionDto();
                inConditionDto.setFindvalue1(map1.get("findvalue1")==null? null:map1.get("findvalue1").toString());
                inConditionDto.setFindvalue2(map1.get("findvalue2")==null? null:map1.get("findvalue2").toString());
                inConditionDto.setId(Long.parseLong(map1.get("id").toString()));
                SearchCondition searchCondition = new SearchCondition();
                searchCondition.setId(inConditionDto.getId());
                List<SearchCondition> list = searchConditionMapper.find(searchCondition);
                if(list.size()>0){
                    SearchCondition s = list.get(0);
                    if(s.getCcondition().equals("like")){
                        if(inConditionDto.getFindvalue1()!=null){
                            sql.append(" and "+s.getCfindfield1()+" like "+"'%"+inConditionDto.getFindvalue1()+"%'");
                        }
                        if(inConditionDto.getFindvalue2()!=null){
                            sql.append(" and "+s.getCfindfield2()+" like "+"'%"+inConditionDto.getFindvalue2()+"%'");
                        }
                    }
                    else if (s.getCcondition().equals("between")){
                        sql.append(" and "+s.getCfindfield1()+" between '"+inConditionDto.getFindvalue1()+"' and '"+inConditionDto.getFindvalue2()+"'");
                    }
                    else {
                        if(inConditionDto.getFindvalue1()!=null){
                            sql.append(" and "+s.getCfindfield1()+" "+s.getCcondition()+" '"+inConditionDto.getFindvalue1()+"'");
                        }
                        if(inConditionDto.getFindvalue2()!=null){
                            sql.append(" and "+s.getCfindfield2()+" "+s.getCcondition()+" '"+inConditionDto.getFindvalue2()+"'");
                        }
                    }
                }
            }
        }
        m.put("msg","操作成功");
        m.put("data",phoneMapper.pageList(sql.toString()));
        return m;
    }

    @Override
    public Map<String, Object> queryPhoneFields(Map<String, Object> map) {
        Map<String,Object> m = new HashMap<>();
        m.put("sessionid", "");
        m.put("data", "");
        if (StringUtil.isBlank(map.get("formId"))){
            m.put("result", 101);
            m.put("msg","formId为空");
            return m;
        }
        m.put("result", 1);
        m.put("msg","操作成功");
        m.put("data", phoneMapper.queryPhoneFields(map));
        return m;
    }

    @Override
    public Map<String, Object> queryDataById(Map<String, Object> map) {
        Map<String,Object> m = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<>();
        if (StringUtil.isBlank(map.get("formId"))){
            m.put("result", 101);
            m.put("msg","formId为空");
            return m;
        }
        if (StringUtil.isBlank(map.get("id"))){
            m.put("result", 102);
            m.put("msg","id为空");
            return m;
        }
        String tableName = phoneMapper.queryFormName(map);
        if(StringUtil.isBlank(tableName)){
            m.put("result", 103);
            m.put("msg","表单不存在");
            return m;
        }
        StringBuffer sql = new StringBuffer();
        sql.append(phoneMapper.queryFields(map));
        sql.append(" from ");
        sql.append(tableName);
        sql.append(" where id = "+map.get("id"));
        Map<String,Object> res = phoneMapper.queryDataById(sql.toString());
        if(res==null){
            m.put("result", 104);
            m.put("msg","数据为空");
            return m;
        }
        for (Map<String,Object> map1: phoneMapper.queryPhoneFields(map)) {
            Map<String,Object> map2 = new HashMap<>();
            map2.put("key",map1.get("cfieldName"));
            map2.put("value",res.get(map1.get("cvalue")));
            list.add(map2);
        }
        m.put("data",list);
        return m;
    }

}
